﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class ArticleList : YuanlinUserControl,
        IReplyConverter<NewsCategoryReply, GetOperation>
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private NewsCategory newsCategory;

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!IsPostBack)
            {
                requestBinders.Add(RequestHelper.BuildGetNewsCategoryRequest(this));
            }
        }

        private void BindData()
        {
            NewsListRequest request = new NewsListRequest();
            if (newsCategory != null)
            {
                if (newsCategory.UpNewsCategoryId.Value > 0)
                {
                    request.ChildNewsCategoryId = newsCategory.EntityId.Value;
                }
                else
                {
                    request.NewsCategoryId = newsCategory.EntityId.Value;
                }
            }
            request.Title = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.QueryText);
            request.PageIndex = pager.PageIndex;
            request.PageSize = pager.PageSize;
            EntityList newss = BusinessManager.GetNewsList(RequestHelper.BuildGetNewsListRequest(request));

            list.DataSource = newss;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(newss.TotalCount);
                currentPage.TotalRecords = (newss.TotalCount);
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

        #region IReplyConverter<NewsCategoryReply,GetOperation> Members

        public void ConvertReply(NewsCategoryReply reply, GetOperation operation)
        {
            if (reply != null)
            {
                newsCategory = reply.Entity as NewsCategory;

                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}
